package com.green.business.controller;

import com.green.business.domain.Sales;
import com.green.business.service.SalesService;
import com.green.business.vo.OpertorTime;
import com.green.business.vo.SaleVo2;
import com.green.business.vo.SalesVo;
import com.green.system.common.ActiveUser;
import com.green.system.common.DataGridView;
import com.green.system.common.ResultObj;
import com.green.system.common.SaleFeedBack;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Green
 * @Date: 2021/10/22/16:31
 * @Description:
 */

@RequestMapping("sales")
@RestController
public class SalesController {

    @Autowired
    private SalesService salesService ;


    /**
     * 开始收银
     */
    @RequestMapping("startSales")
    public Object startSales(){
        return new DataGridView(1L,"");
    }

    /**
     * 查询当前最大的交易id
     */
    @RequestMapping("queryMaxProjectById")
    public Integer queryMaxProjectById(){
        Integer a = this.salesService.queryMaxProjectById();
        if (a == null || a.equals("")){
            return 1;
        }
        return a+1;
    }

    /**
     * 收银成功填补信息
     */
    @RequestMapping("saveSaleInformation")
    public Object saveSaleInformation(SaleVo2 sale){
        Sales sales = new Sales();
        sales.setCustomerid(sale.getCustomerid());
        sales.setPaytype(sale.getPaytype());
        sales.setNumber(sale.getNumber());
        sales.setPrice(sale.getPrice());
        sales.setMemberprice(sale.getMemberprice());
        sales.setGoodsid(sale.getId());
        sales.setProjectid(sale.getProjectid());
        sales.setSalestime(new Date());

        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        sales.setOperateperson(activeUser.getUser().getName());
        this.salesService.saveSales(sales);
        return new SaleFeedBack(200,"item add");
    }

    /**
     * 查询
     */
    @RequestMapping("loadAllSales")
    public Object loadAllSales(SalesVo salesVo){
        return this.salesService.queryAllSales(salesVo);
    }

    /**
     *  操作人以及时间
     * @return
     */
    @RequestMapping("salesOperator")
    public OpertorTime salesOperator(){
        Date d =new Date();
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(d);
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        return new OpertorTime(activeUser.getUser().getName(),dateNowStr);
    }

    /**
     * 添加
     */
    @RequestMapping("addSales")
    public ResultObj addSales(Sales sales){
        try{
            ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
            sales.setOperateperson(activeUser.getUser().getName());
            this.salesService.saveSales(sales);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }

    }

    /**
     * 更新
     */
    @RequestMapping("updateSales")
    public ResultObj updateSales(Sales sales){
        try{
            this.salesService.updateSales(sales);  //update() updateSelective()
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }

    }

    /**
     * 删除
     */
    @RequestMapping("deleteSales")
    public ResultObj deleteSales(Integer id){
        try {
            this.salesService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除
     */
    @RequestMapping("batchDeleteSales")
    public ResultObj batchDeleteSales(Integer[] ids){
        try {
            if (null != ids && ids.length > 0){
                List<Integer> idsList = new ArrayList<>();
                for (Integer id : ids) {
                    idsList.add(id);
                }
                this.salesService.removeByIds(idsList);
                return ResultObj.DELETE_SUCCESS;
            }else{
                return new ResultObj(-1,"传入ID不能为空");
            }

        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
}
